package ebusiness.ebusiness.Dao.OrderDao;

import ebusiness.ebusiness.Dao.DBManager;
import ebusiness.ebusiness.Entity.Order;
import ebusiness.ebusiness.Entity.OrderDetail;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.aspectj.weaver.ast.Or;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class OrderQueryVO {
   public Order findOrderById(int id) throws Exception{
       SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
       try {
           OrderInter orderInter=sqlSession.getMapper(OrderInter.class);
           Order order=orderInter.findOrderById(id);
           sqlSession.close();

           return  order;
       }catch (Exception e){
           e.printStackTrace();
           sqlSession.close();
       }
        return null;

    }
 public   List<Order>  findOrderByDuration(Date from, Date to) throws  Exception{
       SqlSession sqlSession=DBManager.GetSqlFactory().openSession();

     try {

         OrderInter orderInter=sqlSession.getMapper(OrderInter.class);
         java.sql.Date dt=new java.sql.Date(from.getTime());
         java.sql.Date dt2=new java.sql.Date(to.getTime());
         List<Order> orderList=(List<Order>) orderInter.findOrderByDuration2(dt.toString(),dt2.toString());
         sqlSession.close();
         return orderList;
     }catch (Exception e){
         e.printStackTrace();
         sqlSession.close();
     }
       return null;
    }
   public List<Order> findOrderByDay(int day) throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
       try {
           OrderInter orderInter=sqlSession.getMapper(OrderInter.class);
           Date date=new Date();
           Calendar calendar = Calendar.getInstance();
           calendar.setTime(date);int x;
           calendar.add(Calendar.DATE, -day );
           java.sql.Date dt=new java.sql.Date(calendar.getTime().getTime());
           java.sql.Date dt2=new java.sql.Date(date.getTime());
           List<Order> orderList=orderInter.findOrderByDuration2(dt.toString(),dt2.toString());
           sqlSession.close();
           return orderList;
       }catch (Exception e){
           sqlSession.close();
           e.printStackTrace();
       }
        return null;

    }
   public List<Order> findOrderByUId(int uid) throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
       try {
           OrderInter orderInter=sqlSession.getMapper(OrderInter.class);
           List<Order> orderList= orderInter.findOrderByUId(uid);
           sqlSession.close();
           return orderList;
       }catch (Exception e){
           e.printStackTrace();
           sqlSession.close();
       }
       return null;
    }
    public List<Order> findOrderByUId(int uid,int day) throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        try {
            OrderInter orderInter=sqlSession.getMapper(OrderInter.class);
            Date date=new Date();

            Calendar calendar=Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE,-day);
            java.sql.Date dt=new java.sql.Date(calendar.getTime().getTime());
            java.sql.Date dt2=new java.sql.Date(date.getTime());
            List<Order> orderList=(List<Order>) orderInter.findOrderByDuration(uid,dt.toString(),dt2.toString());
            sqlSession.close();
            return orderList;
        }catch (Exception e){
            sqlSession.close();
            e.printStackTrace();
        }
        return null;
    }
   public List<Order> findOrderByDuration(int uid,Date from,Date to) throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
       try {
           OrderInter orderInter=sqlSession.getMapper(OrderInter.class);
           java.sql.Date dt=new java.sql.Date(from.getTime());
           java.sql.Date dt2=new java.sql.Date(to.getTime());
           List<Order> orderList= orderInter.findOrderByDuration(uid,dt.toString(),dt2.toString());
           sqlSession.close();
           return orderList;
       }catch (Exception e){
           e.printStackTrace();
           sqlSession.close();
       }
       return null;
    }

    public List<OrderDetail> findDetailByUID( int uid) throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        try {
            OrderInter orderInter=sqlSession.getMapper(OrderInter.class);
            List<OrderDetail> orderList=orderInter.findDetailByUID(uid);
            sqlSession.close();
            return orderList;
        }catch (Exception e){
            sqlSession.close();
            e.printStackTrace();
        }
       return null;
    }
  public   List<OrderDetail> findDetailByDuration( int uid, Date from, Date to)throws Exception{
      SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
      try {
          OrderInter orderInter=sqlSession.getMapper(OrderInter.class);
          java.sql.Date dt=new java.sql.Date(from.getTime());
          java.sql.Date dt2=new java.sql.Date(to.getTime());
          List<OrderDetail> details=orderInter.findDetailByDuration(uid, dt.toString(), dt2.toString());
          sqlSession.close();
          return details;
      }catch (Exception e){
          sqlSession.close();
          e.printStackTrace();
      }return null;
  };
  public   List<OrderDetail> findDetailByDay( int uid, int day) throws Exception{
      if(day>=0)throw new Exception("DAY >= 0");
      Date date=new Date();
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);int x;
      calendar.add(Calendar.DATE, -day );

      return findDetailByDuration(uid,calendar.getTime(),date);
  };
   public List<OrderDetail> findDetailByID(int id, int uid) throws Exception{
       SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
       try {
           OrderInter orderInter=sqlSession.getMapper(OrderInter.class);
           List<OrderDetail> details=orderInter.findDetailByID(id, uid);
           sqlSession.close();
           return details;
       }catch (Exception e){
          e.printStackTrace();
          sqlSession.close();
       }return null;
   };
   public List<OrderDetail> findDetailByAdmin( int oid) throws Exception{
       SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
       try {
           OrderInter orderInter=sqlSession.getMapper(OrderInter.class);
           List<OrderDetail> details=orderInter.findDetailByAdmin(oid);
           sqlSession.close();
           return details;
       }catch (Exception e){
           sqlSession.close();
           e.printStackTrace();
       }return null;
   };
   public List<OrderDetail> findDetailByAdminDuration( Date from, Date to) throws Exception{
       SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
       try {
           OrderInter orderInter=sqlSession.getMapper(OrderInter.class);

           java.sql.Date dt=new java.sql.Date(from.getTime());
           java.sql.Date dt2=new java.sql.Date(to.getTime());
           List<OrderDetail> details=orderInter.findDetailByAdminDuration( dt.toString(), dt2.toString());
           sqlSession.close();
           return details;
       }catch (Exception e){
           sqlSession.close();
          e.printStackTrace();
       }return null;
   };
  public   List<Order> getUnpayOrder(int uid,String status)throws Exception{
      SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
      try {
          OrderInter orderInter=sqlSession.getMapper(OrderInter.class);
          List<Order> details=orderInter.getUnpayOrder(uid,status);
          sqlSession.close();
          return details;
      }catch (Exception e){
          sqlSession.close();
          e.printStackTrace();
      }return null;

  };
}
